import json
from typing import List, Tuple, Any

from pyecharts.charts import Map
from pyecharts.options import *

# 读取文件
f = open("D:/桌面/可视化案例数据/地图数据/疫情.txt", "r", encoding="UTF-8")
data = f.read()  # 全部数据
# 关闭文件
f.close()
# 取到各省数据
# 将字符串json转换为Python的字典
data_dict = json.loads(data)  # 基础数字字典
# 从字典中取出省份的数据
province_data_list = data_dict["areaTree"][0]["children"]
# 组装每个省份和确诊人数为元组，并各个省的数据都封装入列表内
data_list = []  # 绘图要用的数据列表
for province_data in province_data_list:
    province_name1 = province_data["name"]
    if province_name1 == "上海" or province_name1 == "重庆" or province_name1 == "北京" or province_name1 == "天津":
        province_name1 = province_name1 + "市"
    else:
        province_name1 = province_name1 + "省"
    province_name = province_name1  # 省份名称
    province_confirm = province_data["total"]["confirm"]  # 确诊人数
    data_list.append((province_name, province_confirm))



# 创建地图对象
map = Map()

# 添加数据
map.add("各省份确证人数",data_pair=data_list, maptype="china")

# 设置全局配置，定制分段的视觉映射
map.set_global_opts(
    title_opts=TitleOpts(title="全国疫情地图"),
    visualmap_opts=VisualMapOpts(
        is_show=True,
        is_piecewise=True,
        pieces=[
            {"min":1,"max":99,"lable":"1-99","color":"#CCFFF"},
            {"min":100,"max":999,"lable":"100-999","color":"#FFFF99"},
            {"min":1000,"max":4999,"lable":"1000-4999","color":"#FF9966"},
            {"min":5000,"max":9999,"lable":"5000-9999","color":"#FF6666"},
            {"min":10000,"max":99999,"lable":"10000-99999","color":"#CC3333"},
            {"min":100000,"lable":"10w+","color":"#990033"},

        ]

    )
)

# 绘图
map.render("全国疫情地图.html")
